package com.talent.codes.handler;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.talent.cloud.core.crypto.Base64;
import com.talent.cloud.core.helper.AppFileHelper;
import com.talent.cloud.core.model.RestResult;
import com.talent.cloud.core.model.Result;
import com.talent.cloud.core.page.PageResultHelper;
import com.talent.cloud.core.util.FileUtil;
import com.talent.cloud.core.util.TimeHelper;
import com.talent.cloud.core.util.UuidUtil;
import com.talent.cloud.web.annotation.RestMethod;
import com.talent.codes.model.GasIdCtrl;
import com.talent.codes.model.GasLabel;
import com.talent.codes.model.PrintSetting;
import com.talent.codes.utils.GasLabelGenerator;
import com.talent.codes.utils.JsonConfigHelper;

@RestController
@RequestMapping("/admin/config")
//@RestGroup("系统配置管理")
public class ConfigController
{
    @RestMethod(value = "系统配置查询")
    @GetMapping("/query")
    public Result queryLabels()
    {
        List<GasLabel> list = JsonConfigHelper.listLabels();
        return PageResultHelper.toDataTable(list);
    }

    @RestMethod(value = "系统配置查询")
    @GetMapping("/info")
    public RestResult<GasLabel> queryDetail(GasLabel label)
    {
        RestResult<GasLabel> result = new RestResult<>();
        result.setData( JsonConfigHelper.getLabel(label.getName()) );
        return result;
    }

    @RestMethod(value = "系统配置保存")
    @PostMapping("/save")
    public Result save(@RequestBody GasLabel label)
    {
        JsonConfigHelper.updateLabel(label);
        return new Result();
    }

    @PostMapping("/setting")
    public Result saveSetting(@RequestParam(name = "name") String name, @RequestBody PrintSetting setting)
    {
        JsonConfigHelper.updatePrintSetting(name,setting);
        return new Result();
    }

    @RestMethod(value = "系统配置预览")
    @PostMapping("/preview")
    public Result preview(@RequestBody GasLabel label)
    {
        Result result = new Result();
        try {
            GasIdCtrl idCtrl = new GasIdCtrl(label, 987654321012l, 1);
            idCtrl.next();
            byte[] bbb = idCtrl.curLabelData();
            String b64Data = Base64.encode(bbb);
            result.put("preview", b64Data);
        }catch (Exception e) {
            result = Result.failure("生成预览图失败：" + e.getMessage());
        }

        return result;
    }

    @RestMethod(value = "系统配置预览")
    @GetMapping("/preprint")
    public Result preview0(GasLabel label)
    {
        Result result = new Result();
        try {
            GasLabel gl = JsonConfigHelper.getLabel(label.getName());
            PrintSetting ps = JsonConfigHelper.getPrintSetting(label.getName());
            PrintSetting ps1 = new PrintSetting(ps.getImageW(), ps.getImageH(), ps.getColumns());
            ps1.idCtrl(ps.getFirstId(),ps.getIdCnt());
            ps1.divide(ps.isEmptyBox(), ps.isDividerH(), ps.isDividerV());
            if(ps1.getIdCnt()>100) ps1.setIdCnt(100);

            byte[] bbb = GasLabelGenerator.writeToImage( gl, ps1);
            String b64Data = Base64.encode(bbb);
            result.put("setting", ps);
            result.put("preview", b64Data);
        }catch (Exception e) {
            result = Result.failure("生成预览图失败：" + e.getMessage());
        }

        return result;
    }

    @RestMethod(value = "系统配置删除")
    @PostMapping("/delete")
    public Result delete(@RequestBody GasLabel label) {
        JsonConfigHelper.removeLabel(label.getName());
        return new Result();
    }


    @RestMethod(value = "系统配置预览")
    @GetMapping("/download")
    public Result download(GasLabel label)
    {
        Result result = new Result();
        String tpath = null;
        try {
            String dt = TimeHelper.getCurrentDatetime("yyyyMMddHHmm");
            GasLabel gl = JsonConfigHelper.getLabel(label.getName());
            PrintSetting ps = JsonConfigHelper.getPrintSetting(label.getName());
            String title = gl.getTitle().trim().replace("　","");
            String uuid = UuidUtil.getUUID16();
            String tfn = uuid + ".jpg";
            String dfn = uuid + ".pdf";
            long lastId = ps.getFirstId() + ps.getIdCnt() - 1;
            String fileName = title + "-" + ps.getFirstId() + "-" + lastId + ".pdf";

            tpath = AppFileHelper.getSysFileTmpPath() + tfn;
            String dpath = AppFileHelper.getSysFileTmpPath() + dfn;

            GasLabelGenerator.writeToImage(tpath, gl, ps );
            GasLabelGenerator.saveToPdf(dpath, tpath);
            long nextId = lastId + 1;
            JsonConfigHelper.printSuccess(label.getName(), nextId);


            result.put("fileName", dfn);
            result.put("fileType", "tmp");
            result.put("realName", fileName);
        }catch (Exception e) {
            result = Result.failure("生成Pdf失败：" + e.getMessage());
        }finally {
            if(tpath!=null) {
                FileUtil.delete(tpath);
            }
        }

        return result;
    }

}
